package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysFlowNode;
import com.ysd.lis.entity.SysFlowNodeApprover;
import com.ysd.lis.entity.SysFlowNodeEvent;
import com.ysd.lis.mapper.sys.SysFlowNodeApproverMapper;
import com.ysd.lis.mapper.sys.SysFlowNodeEventMapper;
import com.ysd.lis.mapper.sys.SysFlowNodeMapper;
import com.ysd.lis.request.SysFlowParam;
import com.ysd.lis.service.SysFlowNodeApproverService;
import com.ysd.lis.service.SysFlowNodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2024-04-01
 */
@Service
@Slf4j
public class SysFlowNodeServiceImpl extends ServiceImpl<SysFlowNodeMapper, SysFlowNode> implements SysFlowNodeService {


    @Autowired
    SysFlowNodeMapper sysFlowNodeMapper;
    @Autowired
    SysFlowNodeApproverMapper sysFlowNodeApproverMapper;
    @Autowired
    SysFlowNodeApproverService sysFlowNodeApproverService;
    @Autowired
    SysFlowNodeEventMapper sysFlowNodeEventMapper;
    @Override
    public Result findNodeList(SysFlowParam param) {
        String definitionId = param.getDefinitionId();
        LambdaQueryWrapper<SysFlowNode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysFlowNode::getDefinitionId, definitionId).eq(SysFlowNode::getDelFlag, 0).orderByAsc(SysFlowNode::getSeq);
        List<SysFlowNode> nodeList = sysFlowNodeMapper.selectList(lambdaQueryWrapper);

        if (CollectionUtils.isNotEmpty(nodeList)) {
            List<String> collect = nodeList.stream().map(SysFlowNode::getId).collect(Collectors.toList());
            LambdaQueryWrapper<SysFlowNodeApprover> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(SysFlowNodeApprover::getNodeId, collect).eq(SysFlowNodeApprover::getDelFlag, 0);
            List<SysFlowNodeApprover> approverList = sysFlowNodeApproverMapper.selectList(queryWrapper);
            for (SysFlowNodeApprover approver : approverList) {
                int a = collect.indexOf(approver.getNodeId());
                if (a >= 0) {
                    SysFlowNode node = nodeList.get(a);
                    List<String> approverIds = node.getApproverIds();
                    if (null == approverIds) {
                        approverIds = new ArrayList<>();
                        node.setApproverIds(approverIds);
                    }
                    approverIds.add(approver.getApproverId());
                }
            }
        }
        return Result.succ(1, "查询成功", nodeList);
    }

    @Override
    public Result addNode(SysFlowNode tFlowNode) {
        int i = sysFlowNodeMapper.insert(tFlowNode);
        if (i > 0) {
            List<String> userIds = tFlowNode.getApproverIds();
            List<SysFlowNodeApprover> nodeRoleList = new ArrayList<>();
            for (String userId : userIds) {
                SysFlowNodeApprover nodeRole = new SysFlowNodeApprover();
                nodeRole.setNodeId(tFlowNode.getId());
                nodeRole.setApproverId(userId);
                nodeRole.setDefinitionId(tFlowNode.getDefinitionId());
                nodeRoleList.add(nodeRole);
            }
            sysFlowNodeApproverService.saveBatch(nodeRoleList);
            return Result.succ(1, "新增成功", "");
        }
        return Result.succ(400, "新增失败", "");
    }

    @Override
    public Result editNode(SysFlowNode tFlowNode) {
        int i = sysFlowNodeMapper.updateById(tFlowNode);
        if (i > 0) {
            //删除角色中间表 然后插入
            LambdaQueryWrapper<SysFlowNodeApprover> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysFlowNodeApprover::getNodeId, tFlowNode.getId());
            sysFlowNodeApproverMapper.delete(queryWrapper);
            List<String> userIds = tFlowNode.getApproverIds();
            List<SysFlowNodeApprover> nodeRoleList = new ArrayList<>();
            for (String userId : userIds) {
                SysFlowNodeApprover nodeRole = new SysFlowNodeApprover();
                nodeRole.setNodeId(tFlowNode.getId());
                nodeRole.setApproverId(userId);
                nodeRole.setDefinitionId(tFlowNode.getDefinitionId());
                nodeRoleList.add(nodeRole);
            }
            sysFlowNodeApproverService.saveBatch(nodeRoleList);
            return Result.succ(1, "修改成功", "");
        }
        return Result.succ(400, "修改失败", "");
    }

    @Override
    public Result deleteNode(SysFlowNode node) {
        String id = node.getId();
        //删除此节点时先查询是否有其他节点下的事件的下一节点是否是此节点
        LambdaQueryWrapper<SysFlowNodeEvent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysFlowNodeEvent::getNextNode,id).eq(SysFlowNodeEvent::getDelFlag,0);
        Integer selectCount = sysFlowNodeEventMapper.selectCount(queryWrapper);
        if(selectCount>0){
            return Result.succ(400, "有其他节点下的事件指向此节点，不能删除", "");
        }
        int deleteById = sysFlowNodeMapper.deleteById(id);
        if(deleteById>0){
            //删除节点下的审批人员信息
            LambdaQueryWrapper<SysFlowNodeApprover> delApproverWrapper = new LambdaQueryWrapper<>();
            delApproverWrapper.eq(SysFlowNodeApprover::getNodeId,id);
            sysFlowNodeApproverMapper.delete(delApproverWrapper);
            //删除节点下所有的事件
            LambdaQueryWrapper<SysFlowNodeEvent> delEventWrapper = new LambdaQueryWrapper<>();
            delEventWrapper.eq(SysFlowNodeEvent::getNodeId,id);
            sysFlowNodeEventMapper.delete(delEventWrapper);
            return Result.succ(1, "删除成功", "");
        }else{
            return Result.succ(400, "删除失败", "");
        }
    }
}
